package com.baicun.wish.spiders;

import com.baicun.wish.sink.SinkToCsv;
import com.baicun.wish.utils.driver.WebDriverUtil;
import com.sun.org.apache.xml.internal.res.XMLErrorResources_tr;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

import java.util.List;

/**
 * @author baicun
 * @description 负责爬取 https://gkcx.eol.cn/linepro 各省录取分数线
 */
public class ProvinceAdmitSpider {

    public static void getScoreLine() throws InterruptedException {

        SinkToCsv.create("province_admit.csv");
        SinkToCsv.writeHeader(new String[]{"province","year","exam_type","admit_batch","score_line"});

        ChromeDriver chromeDriver = WebDriverUtil.openChrome();

        chromeDriver.get("https://gkcx.eol.cn/linepro");

        List<WebElement> webElements = chromeDriver.findElements(By.className("listSpan"));

        List<WebElement> provinces = webElements.get(0).findElement(By.className("more-search")).findElements(By.tagName("span"));
        for (WebElement province : provinces){
            System.out.println(province.getText());
            province.click();
            List<WebElement> newElements = chromeDriver.findElements(By.className("listSpan"));
            List<WebElement> years = newElements.get(3).findElement(By.className("more-search")).findElements(By.tagName("span"));
            for (WebElement year : years){
                System.out.println(year.getText());
                year.click();
                Thread.sleep(800);
                WebElement tbody = chromeDriver.findElement(By.className("school-tbody"));
                List<WebElement> trs = tbody.findElements(By.tagName("tr"));

                for (WebElement tr : trs){
                    List<WebElement> tds = tr.findElements(By.tagName("td"));

                    System.out.println(
                                    tds.get(0).getText()+","+
                                    tds.get(1).getText()+ ","+
                                    tds.get(2).getText()+ "," +
                                    tds.get(3).getText()+","+
                                    tds.get(4).getText()+",");

                    SinkToCsv.writeContent(new String[]{tds.get(0).getText(),tds.get(1).getText(),tds.get(2).getText(),tds.get(3).getText(),tds.get(4).getText()});


                }

            }
        }

        SinkToCsv.close();
    }
}
